﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace AcesHigh.GUI
{

    public delegate void CardClickedEvent(CardPanel panel, Card card);

    public class CardPanel : Panel
    {
        List<Card> cards;
        List<PictureBox> cardImages;

        public event CardClickedEvent CardClicked;

        public CardPanel()
        {
            initLists();
            initPanel();
           
        }

        private void initLists()
        {
            cards = new List<Card>(); this.
            cardImages = new List<PictureBox>();
        }

        private void initPanel()
        {
            this.BackColor = Color.ForestGreen;
            this.Size = new Size(72, 96);
            this.MinimumSize = this.Size;
            this.BackColor = Color.Transparent;
            this.AutoSize = true;
            this.AllowDrop = true;
            this.DragEnter += new DragEventHandler(Drag_DragEnter);
            this.DragDrop += new DragEventHandler(Drag_DragDrop);
            
            this.Controls.Add(new EmptyDeckPictureBox());
        }

        
        public Card TopCard
        {
            get
            {
                if (cards.Count > 0)
                {
                    return cards[cards.Count - 1];
                }
                else
                {
                    return null;
                }
            }
        }

        public bool IsEmpty
        {
            get { return (cards.Count == 0); }
        }

        public PictureBox TopPictureBox
        {
            get { return this.cardImages[cardImages.Count - 1]; }
        }
            
        

        public void addCard(Card card)
        {
            
            PictureBox pb = new PictureBox();
            pb.Size = new Size(72, 96);
            
            pb.Image = new Bitmap(card.Image);
            this.Controls.Add(pb);
            pb.Location = new Point(0, cards.Count * 26);
            pb.BringToFront();
            pb.Focus();
            cards.Add(card);
            cardImages.Add(pb);
            updateClickHandler();
        }

        private void updateClickHandler()
        {
            
            int count = cardImages.Count;

            if (count == 0) return;

            if (count > 1)
            {
                (cardImages[count - 2]).DoubleClick -= cardClicked;
                (cardImages[count - 2]).MouseDown -= CardPanel_MouseDown;
                
            }
            cardImages[count - 1].DoubleClick +=new EventHandler(cardClicked);
            cardImages[count - 1].MouseDown += new MouseEventHandler(CardPanel_MouseDown);
        }

        private void cardClicked(object obj, EventArgs args)
        {
            Console.WriteLine(cards[cards.Count - 1].ToString() + " clicked");
            if (CardClicked != null)
            {
                CardClicked(this, cards[cards.Count - 1]);
            }
           
        }

        public void removeTopCard()
        {
            cards.RemoveAt(cards.Count - 1);
            this.Controls.Remove(cardImages[cardImages.Count - 1]);
            cardImages.RemoveAt(cardImages.Count - 1);
            updateClickHandler();
        }

        public void CardPanel_MouseDown(Object obj, MouseEventArgs args)
        {
            if (args.Clicks == 1)
            {
                PictureBox pictureBox1 = (PictureBox)obj;
                DoDragDrop(this, DragDropEffects.Move);
            }
            else
            {
                cardClicked(obj, args);
            }
        }

        public void Drag_DragEnter(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            Console.WriteLine(cards.Count);
            if (cards.Count == 0)
            {
                if(args.Data.GetDataPresent(typeof(CardPanel)))
                {
                   
                    args.Effect = DragDropEffects.Move;
                } else {
                    args.Effect = DragDropEffects.None;
                }
            }
        }

        public void Drag_DragDrop(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            Console.WriteLine(cards.Count);
            if (cards.Count == 0)
            {
                CardPanel cp = (CardPanel)args.Data.GetData(typeof(CardPanel));
 
                Card c = cp.TopCard;
                cp.removeTopCard();
                this.addCard(c);
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (cards != null) cards = null;
                if (cardImages != null) cardImages = null;
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CardPanel
            // 
            this.AutoSize = true;
            this.Size = new System.Drawing.Size(72, 96);
            this.ResumeLayout(false);

        }
    }
}
